﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace yule.Common.Log
{
    /// <summary>
    /// 系统日志
    /// </summary>
    internal class Log2
    {
        private static object log_lock = new object();
        /// <summary>
        /// 写入日志(yule_log.txt)
        /// </summary>
        /// <param name="message"></param>
        public static void Write(string message)
        {
            string fileName = System.AppDomain.CurrentDomain.BaseDirectory + "\\yule_log.txt";
            lock (log_lock)
            {
                WriteToFile(message, fileName);
            }
        }

        private static object error_lock = new object();
        /// <summary>
        /// 记录错误(yule_error.txt)
        /// </summary>
        /// <param name="message"></param>
        public static void Error(string message)
        {
            string fileName = System.AppDomain.CurrentDomain.BaseDirectory + "\\yule_error.txt";
            lock (error_lock)
            {
                WriteToFile(message, fileName);
            }
        }


        private static void WriteToFile(string message, string fileName)
        {
            if (System.IO.File.Exists(fileName) == false)
            {
                using (FileStream fs = System.IO.File.Create(fileName))
                {
                    message += "\r\n";
                    byte[] bs = System.Text.Encoding.UTF8.GetBytes(message);
                    fs.Write(bs, 0, bs.Length);
                    fs.Close();
                }
            }
            else
            {
                using (StreamWriter sw = File.AppendText(fileName))
                {
                    sw.WriteLine(message);
                    sw.Close();
                }
            }
        }


    }
}
